﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MvcMall.Core.Commands;
using MvcMall.Core.Contacts.Repositories;
using MvcMall.Core.Contacts.Tasks;
using MvcMall.Core.Domains;
using MvcMall.Framework.MvcPager;
using MvcMall.Framework.Utility;

namespace MvcMall.Tasks
{
    public class ArticleTask : IArticleTask
    {
        private readonly IArticleRepository _articleRepository;

        private readonly IArticleCategoryRepository _articleCategoryRepository;

        public ArticleTask(IArticleRepository articleRepository, IArticleCategoryRepository articleCategoryRepository)
        {
            this._articleRepository = articleRepository;
            this._articleCategoryRepository = articleCategoryRepository;
        }


        public Article Save(ArticleCommand cmd)
        {
            if (cmd.ArticleId != null && cmd.ArticleId > 0)
            {
                //更新
                var updateModel = _articleRepository.Get(Convert.ToInt32(cmd.ArticleId));
                if (updateModel != null)
                {
                    updateModel.ArticleCategory = _articleCategoryRepository.Get(Int32.Parse(updateModel.ArticleCategory.Id.ToString()));
                    updateModel.Content = Utils.HtmlEncode(cmd.Content);
                    updateModel.IsShow = cmd.IsShow;
                    updateModel.SortOrder = cmd.SortOrder;
                    updateModel.Title = cmd.Title;
                    return _articleRepository.Update(updateModel);
                }
            }
            var insertModel = new Article()
            {
                ArticleCategory = _articleCategoryRepository.Get(Int32.Parse(cmd.ArticleCategoryId.ToString())),
                Content = Utils.HtmlEncode(cmd.Content),
                CreateTime = DateTime.Now,
                IsShow = cmd.IsShow,
                Title = cmd.Title
            };
            return _articleRepository.Save(insertModel);

        }

        public Article Get(int articleId)
        {
            return _articleRepository.Get(articleId);
        }

        public List<Article> GetAll()
        {
            return _articleRepository.GetAll().ToList();
        }

        public PagedList<Article> GetPagedList(int pageIndex, int pageSize, int catId, string title)
        {
            return _articleRepository.QueryAll()
                .Where(m =>
                       (catId == -1 || m.ArticleCategory.Id == catId)
                       && (string.IsNullOrEmpty(title) || m.Title.Contains(title))
                )
                .OrderByDescending(m => m.CreateTime)
                .ToPagedList(pageIndex, pageSize);
        }

        public PagedList<Article> GetPagedList(int pageIndex, int pageSize)
        {
            return _articleRepository.QueryAll()
                .OrderByDescending(m => m.CreateTime)
                .ToPagedList(pageIndex, pageSize);
        }
    }
}
